Sharpen Your Skills: Programming Languages Practice Q&A

C Language Basic Programming Questions

 
1

Question- Hello World Program in C language.



Answer-
#include <stdio.h> 
#include <conio.h>
void main()
{
//printf() function is used to display string on output screen.
//printf() function definition is define in stdio.h header file
printf("Hello, World!"); return 0;}



C Programming class
2

Question- Write a c program for input an integer number from user and display its value.



Answer-
#include <stdio.h>
void main()
{
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
}
3

Question- Write a c program for input a float number from user and display its value.



Answer-
#include <stdio.h>
void main()
{
float number;
printf("Enter an integer: ");
// reads and stores input
scanf("%f", &number);
// displays output
printf("You entered: %f", number);
}
4

Question- Write a c program for input two integer number from user and display both value.



Answer-
#include <stdio.h>
void main()
{
int num1,num2;
printf("Enter two integer: ");
// reads and stores input
scanf("%d%d", &num1,&num2);
// displays output
printf("You entered 1st number is: %d \t and 2nd number is %d", num1,num2);
}

Learn Programming Language: CSDT Centre
5

Question- Write a c program for input two floating point number from user and display both value.



Answer-
#include <stdio.h>
void main()
{
float num1,num2;
printf("Enter two Float Numbers: ");
// reads and stores input
scanf("%f%f", &num1,&num2);
// displays output
printf("You entered 1st number is: %f \t and 2nd number is %f", num1,num2);
}

Learn Programming Language: CSDT Centre
6

Question- Write a c program for input a character from user and display its value.



Answer-
#include <stdio.h>
void main()
{
char ch;
printf("Enter an character: ");
scanf("%c", &ch);
// displays output
printf("You entered: %c", ch);
}
7

Question- Write a c program for input two integer number from user and display sum.



Answer-
#include <stdio.h>
void main()
{
int num1,num2,s;
printf("Enter two integer: ");
scanf("%d%d", &num1,&num2);
printf("You entered 1st number is: %d \t and 2nd number is %d\n", num1,num2);

s=num1+num2;
printf
("Sum is %d",s);
}

Learn Programming Language: CSDT Centre
8

Question- Write a c program for input two integer number from user and display its subtraction, multiplication, division and reminder.



Answer-
#include <stdio.h>
void main()
{
int num1,num2;
printf("Enter two integer: ");
scanf("%d%d", &num1,&num2);
printf("You entered 1st number is: %d \t and 2nd number is %d", num1,num2);
printf("\n Subtraction is %d",(num1-num2));
printf("\n Multiplication is %d",(num1*num2));
printf("\n Division is %d",(num1/num2));
printf("\n Reminder is %d",(num1%num2));
}

Learn Programming Language: CSDT Centre
9

Question- Write a C Program to find the ASCII value of a character.



Answer-
#include <stdio.h>
void main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
}

Output:
Enter a character: A
ASCII value of A is = 65

10

Question- Write a C Program to find size of Variable or datatype.



Answer-
#include<stdio.h>
void main()
{
int i;
float f;
double d;
char c;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(i));
printf("Size of float: %zu bytes\n", sizeof(f));
printf("Size of double: %zu bytes\n", sizeof(d));
printf("Size of char: %zu byte\n", sizeof(c));
}

Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
11

Question- Write a C Program to enter 2 number and swap its value.



Answer- #include<stdio.h>
void main()
{
int n1,n2,T;
printf("Enter two Numbers\n");
scanf("%d%d",&n1,&n2);
printf("Enter 1st number is %d",n1);
printf("Enter 2nd number is %d",n2);
T=n1;
n1=n2;
n2=T;
printf("After Swap 1st number is %d",n1);
printf("After Swap 2nd number is %d",n2);
}
12

Question- Write a C Program to enter 2 number and swap its value without using third variable.



Answer-  #include<stdio.h>
 void main()
 {
 int n1,n2;
 printf("Enter two Numbers\n");
 scanf("%d%d",&n1,&n2);
 printf("Enter 1st number is %d",n1);
 printf("Enter 2nd number is %d",n2);

 n1=n1+n2;
  n2=n1-n2;
  n1=n1-n2;
 printf("After Swap 1st number is %d",n1);
 printf("After Swap 2nd number is %d",n2);
 }

13

Question- Write a C Program to enter cost of a pen from user and find similar 10 pen cost with 10 percentage discount. .



Answer-
#include<stdio.h>
 void main()
 {
 int pen,Ten_Pen,dis,PayAmt;
 printf("Enter Cost of Pen.\n");
 scanf("%d",&pen);
 Ten_Pen=10*pen;
 dis=(Ten_Pen)*10/100;
 PayAmt=Ten_Pen-dis;
 printf("One Pen Cost is %d",pen);
 printf("\n Ten Pen Cost is %d",Ten_Pen);
  printf("\n  Discount Cost is %d",dis);
 printf("\n paying
 Pen Cost is %d",Pay_Amt);
 }
14

Question- Write a C Program to enter radius of circle and find AREA .



Answer- #include <stdio.h>

void main()
{

  float radius, area;

  printf("Enter the radius of a circle\n");

  scanf("%f", &radius);

  area = 3.14159*radius*radius;

  printf("Area of the circle = %.2f\n", area);

  
}